package com.hinnotek.amoeba.ui.activity.old

import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import com.lee.common.base.BaseActivity
import com.hinnotek.amoeba.R
import com.hinnotek.amoeba.base.BaseBean
import com.hinnotek.amoeba.bean.old.AmibaInstallByIdBean
import com.hinnotek.amoeba.databinding.ActivityMsgPaymentRequestDetailBinding
import com.hinnotek.amoeba.dialog.CustomDialog
import com.hinnotek.amoeba.utilities.IntentUtils
import com.hinnotek.amoeba.utilities.ToastUtils
import com.hinnotek.amoeba.viewmodels.NoticeViewModel
import com.lee.common.network.net.ObserverState
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MsgPaymentRequestDetailActivity : BaseActivity<ActivityMsgPaymentRequestDetailBinding>(),
    View.OnClickListener {

    private var amibaReimburseId = 0
    private var noticeId = 0
    private var applyUserId = 0
    private var approvalUserId: String? = null

    private val viewModel by viewModels<NoticeViewModel>()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setLeftVisible(true)
        setTitle("采购")

        setContentView(R.layout.activity_msg_payment_request_detail)
    }

    override fun initVew() {
        bindingView.superButtonHistory.setOnClickListener(this)
        bindingView.superButtonReject.setOnClickListener(this)
        bindingView.superButtonAgree.setOnClickListener(this)
    }

    override fun setData() {
        val type = intent.getIntExtra("type", 0)
        noticeId = intent.getIntExtra("noticeId", 0)
        if (type == 0) {
            bindingView.llMsgBtns.visibility = View.VISIBLE
        } else if (type == 1) {
            bindingView.llMsgBtns.visibility = View.GONE
        } else {
            bindingView.superButtonReject.visibility = View.GONE
            bindingView.superButtonAgree.visibility = View.GONE
        }
        getNoticeDetailById(noticeId)
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.superButton_history -> {
                val bundle = Bundle()
                bundle.putInt("msgType", 2)
                bundle.putInt("applyUserId", applyUserId)
                bundle.putString("approvalUserId", approvalUserId)
                IntentUtils.startActivity(mContext, MsgHistoryListActivity::class.java, bundle)
            }
            R.id.superButton_reject -> amibaReimburseApproval(amibaReimburseId, 3, noticeId)
            R.id.superButton_agree -> amibaReimburseApproval(amibaReimburseId, 2, noticeId)
        }
    }

    /**
     * app端通过id查询单个临时缴费续费申请接口
     */
    fun getNoticeDetailById(noticeId: Int) {
        val customDialog = CustomDialog(mContext, "正在加载...")
        customDialog.show()

        viewModel.getNoticeDetailById(noticeId)
            .observe(this, object : ObserverState<AmibaInstallByIdBean>() {
                override fun onDataChange(data: AmibaInstallByIdBean) {

                    applyUserId = data.applyUserId
                    approvalUserId = data.approvalUserId
                    amibaReimburseId = data.amibaPayId
                    bindingView.tvPaymentType.setText(if (data.amibaPayType == 1) "产品" else "项目")
                    if (data.amibaPayType == 1) {
                        bindingView.llPaymentChooseUnit.visibility = View.GONE
                        bindingView.tvPaymentChooseProject.setText(data.provideProjectName)
                    } else {
                        bindingView.llPaymentChooseUnit.visibility = View.VISIBLE
                        bindingView.tvPaymentChooseUnit.setText(data.customerName)
                        bindingView.tvPaymentChooseProject.setText(data.projectName)
                    }

                    bindingView.tvPaymentHopeDate.text = data.payDate
                    bindingView.tvPaymentCause.text = data.payOverview
                    bindingView.tvPaymentTotal.text = data.payTotal
                    bindingView.tvApplicant.text = data.createUser
                    bindingView.tvRelatedApprover.setText(data.approvalUser)
                    if (data.approvalState == 1) {
                        bindingView.tvApproverStatus.text = "已申请"
                        with(bindingView) { tvApproverStatus.setTextColor(getColor21(R.color.black)) }
                    } else if (data.approvalState == 2) {
                        bindingView.tvApproverStatus.text = "已同意"
                        bindingView.tvApproverStatus.setTextColor(getColor21(R.color.xui_btn_green_normal_color))
                    } else if (data.approvalState == 3) {
                        bindingView.tvApproverStatus.text = "已拒绝"
                        bindingView.tvApproverStatus.setTextColor(getColor21(R.color.xui_config_color_red))
                    }

                    bindingView.tvPaymentCc.text = data.sendUser
                    super.onDataChange(data)
                }

                override fun onError(msg: String?, code: Int) {
                    customDialog.dismiss()
                    ToastUtils.showShort(mContext, msg)
                    super.onError(msg, code)
                }
            })

    }

    /**
     * app端阿米巴付款申请审批接口
     */
    fun amibaReimburseApproval(
        amibaPayId: Int, approvalState: Int,
        noticeId: Int
    ) {
        val customDialog = CustomDialog(mContext, "正在加载...")
        customDialog.show()


        viewModel.getAmibaPayApproval(amibaPayId, approvalState, noticeId)
            .observe(this, object : ObserverState<BaseBean>() {
                override fun onDataChange(data: BaseBean) {
                    customDialog.dismiss()
                    finishActivity()
                    super.onDataChange(data)
                }

                override fun onError(msg: String?, code: Int) {
                    customDialog.dismiss()
                    ToastUtils.showShort(mContext, msg)
                    super.onError(msg, code)
                }
            })
    }
}